package com.example.cake.controller;

import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.cake.common.Result;
import com.example.cake.entity.DTO.SysClassifyDTO;
import com.example.cake.entity.DTO.SysCustomerDTO;
import com.example.cake.entity.SysCake;
import com.example.cake.entity.SysClassify;
import com.example.cake.entity.SysCustomer;
import com.example.cake.service.SysCakeService;
import com.example.cake.service.SysClassifyService;
import com.example.cake.service.SysCustomerService;
import org.apache.logging.log4j.util.Strings;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/customer")
public class SysCustomerController {
    @Autowired
    private SysCustomerService sysCustomerService;


    @PostMapping("/list")
    public Result list(@RequestBody SysCustomerDTO sysCustomerDTO) {
        LambdaQueryWrapper<SysCustomer> lqw = new LambdaQueryWrapper<>();
        lqw.like(Strings.isNotEmpty(sysCustomerDTO.getUsername()),SysCustomer::getUsername,sysCustomerDTO.getUsername());
        lqw.like(Strings.isNotEmpty(sysCustomerDTO.getPhone()),SysCustomer::getPhone,sysCustomerDTO.getPhone());
        lqw.like(Strings.isNotEmpty(sysCustomerDTO.getEmail()),SysCustomer::getEmail,sysCustomerDTO.getEmail());
        Page<SysCustomer> page = new Page<>(sysCustomerDTO.getPage(), sysCustomerDTO.getLimit());
        return Result.success(sysCustomerService.page(page,lqw));
    }

    @PostMapping("/add")
    public Result add(@RequestBody SysCustomer sysCustomer) {
        LambdaQueryWrapper<SysCustomer> lqw = new LambdaQueryWrapper<>();
        lqw.eq(SysCustomer::getUsername,sysCustomer.getUsername());
        SysCustomer one = sysCustomerService.getOne(lqw);
        if(one != null){
            return Result.error("账号已存在，请更换账号","-1");
        }else {
            sysCustomer.setCreateTime(DateUtil.toLocalDateTime(new Date()));
            boolean save = sysCustomerService.save(sysCustomer);
            return Result.success("200","注册成功",save);
        }
    }
    @GetMapping("getOneCustomer")
    private Result getOneCustomer(@RequestParam Integer id){
        LambdaQueryWrapper<SysCustomer> customerLambdaQueryWrapper = new LambdaQueryWrapper<>();
        customerLambdaQueryWrapper.eq(SysCustomer::getId,id);
        SysCustomer one = sysCustomerService.getOne(customerLambdaQueryWrapper);
        return Result.success(one);
    }
    @PostMapping("/remove")
    public Result add(@RequestBody Integer id) {
        return Result.success(sysCustomerService.removeById(id));
    }

    @PostMapping("/update")
    public Result update(@RequestBody SysCustomer sysCustomer) {
        return Result.success(sysCustomerService.updateById(sysCustomer));
    }

    @PostMapping("/login")
    public Result loginByNameAndPassword(@RequestBody SysCustomer sysCustomer) {
        LambdaQueryWrapper<SysCustomer> lqw = new LambdaQueryWrapper<>();
        lqw.eq(SysCustomer::getUsername,sysCustomer.getUsername());
        lqw.eq(SysCustomer::getPassword,sysCustomer.getPassword());
        SysCustomer one = sysCustomerService.getOne(lqw);
        if(one != null){
            return Result.success("200","登录成功",one);
        }else {
            return Result.error("账号或密码错误","-1");
        }
    }

}
